package com.api.controller;

import com.api.annotation.ApiAnnotation;
import com.api.constant.CommonConstant;
import com.api.interceptor.ApiInterceptor;
import com.api.model.TCollect;
import com.api.model.TMerchant;
import com.api.model.TTag;
import com.api.model.TUser;
import com.api.service.CollectService;
import com.api.service.MerchantService;
import com.api.service.MerchantTagService;
import com.api.service.OrderService;
import com.api.utils.DateTimeUtils;
import com.api.utils.JsonReturnUtils;
import com.api.utils.OrderUtils;
import com.api.utils.RollPage;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by PC on 2017/7/12.
 */
@RestController
@RequestMapping("api/collect")
public class CollectController {
    private static final Logger logger = Logger.getLogger(MerchantController.class);
    @Autowired
    private CollectService collectService;
    @Autowired
    private MerchantTagService merchantTagService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private OrderService orderService;

    @ApiAnnotation
    @RequestMapping(value = "list")
    public Object list(Integer page, Integer pageSize) {
        page = (page == null || page == 0) ? 1 : page;
        pageSize = (pageSize == null || pageSize == 0) ? 5 : pageSize;
        TUser user = ApiInterceptor.getUserByRequest();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", user.getId());
        params.put("status", user.getId());
        OrderUtils orderUtils = new OrderUtils();
        orderUtils.pushDesc("create_time");
        RollPage<TCollect> rollPage = collectService.findListPageByParams(params, orderUtils, page, pageSize);
        List<TCollect> collectList = rollPage.getRecordList();
        if (collectList.size() > 0) {
            List<TTag> tagList;
            for (TCollect collect : collectList) {
                params = new HashMap<String, Object>();
                params.put("merchantId", collect.getMerchantId());
                params.put("status", CommonConstant.OrderStatus.Comment.getValue());
                Double score = orderService.selectAverageScoreByParams(params);
                score = score == null ? 0 : score;
                collect.setCommentScore(score);
                tagList = new ArrayList<TTag>();
                params.put("merchantId", collect.getMerchantId());
                tagList = merchantTagService.getTagList(params);
                collect.setTagList(tagList);
            }
        }
        return JsonReturnUtils.success(collectList);
    }

    @ApiAnnotation
    @RequestMapping(value = "success")
    public Object success(Integer merchantId) {
        TUser user = ApiInterceptor.getUserByRequest();
        TMerchant merchant = (TMerchant) merchantService.findObjByKey(merchantId);
        if (merchant == null) {
            return JsonReturnUtils.fail("B001");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", user.getId());
        params.put("merchantId", merchantId);
        params.put("status", CommonConstant.DataStatus.Normal.getValue());
        TCollect collect = (TCollect) collectService.selectOneByParams(params);
        if (collect != null) {
            return JsonReturnUtils.success();
        }
        collect = new TCollect();
        collect.setUserId(user.getId());
        collect.setUserName(user.getName());
        collect.setMerchantId(merchant.getId());
        collect.setMerchantName(merchant.getName());
        collect.setStatus(CommonConstant.DataStatus.Normal.getValue());
        collect.setCreateTime(DateTimeUtils.getTime());
        collect.setUpTime(DateTimeUtils.getTime());
        collectService.insertSelective(collect);
        return JsonReturnUtils.success();
    }

    @ApiAnnotation
    @RequestMapping(value = "cancel")
    public Object cancel(Integer merchantId) {
        TUser user = ApiInterceptor.getUserByRequest();
        TMerchant merchant = (TMerchant) merchantService.findObjByKey(merchantId);
        if (merchant == null) {
            return JsonReturnUtils.fail("B001");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", user.getId());
        params.put("merchantId", merchantId);
        params.put("status", CommonConstant.DataStatus.Normal.getValue());
        TCollect collect = (TCollect) collectService.selectOneByParams(params);
        if (collect == null) {
            return JsonReturnUtils.success();
        }
        collect.setStatus(CommonConstant.DataStatus.Delete.getValue());
        collect.setUpTime(DateTimeUtils.getTime());
        collectService.updateByPrimaryKeySelective(collect);
        return JsonReturnUtils.success();
    }
}
